package com.shwread.android.qysw.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw10000152.R;
import com.shwread.http.base.Log;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;

import logic.util.Util;
import logic.util.Utils;


/**
 * OrderMenuPayChangePasswordActivity
 * 
 * @Description 安全设置——修改支付密码界面
 * @Author zhangdong
 * @Date 2015年7月13日下午14:17:21
 */
public class OrderMenuPayChangePasswordActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener
{
	private Context            context;
	private TextView           tvTitle;
	private ImageView          ivShow;
	private EditText           etOldPwd, etNewPwd, etNewPwdAgain;
	private Button             btnBack, btnSubmit;
	private boolean            Is_SHOW_PWD = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.om_my_wallet_pay_password_exist);
		this.context = this;
		initView();
		setListener();
		initData();
	}
	
	private Handler handler = new Handler()
	                        {
		                        @Override
		                        public void handleMessage(Message msg) {
			                        switch (msg.what) {
			                        
										default:
											break;
									}
								}
	                        };
	
	private void setListener() {
		btnBack.setOnClickListener(this);
		ivShow.setOnClickListener(this);
		btnSubmit.setOnClickListener(this);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	private void initView() {
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setText("修改支付密码");
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		
		etOldPwd = (EditText) findViewById(R.id.et_om_my_wallet_pay_password_old_pwd);
		
		etNewPwd = (EditText) findViewById(R.id.et_om_my_wallet_pay_password_new_pwd);
		
		etNewPwdAgain = (EditText) findViewById(R.id.et_om_my_wallet_pay_password_new_pwd_again);
		
		ivShow = (ImageView) findViewById(R.id.iv_om_my_wallet_pay_password_pwd_show);
		
		btnSubmit = (Button) findViewById(R.id.btn_om_my_wallet_pay_password);
	}
	
	private void initData() {
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				Util.hideInputMethodManager(context, etOldPwd);
				Util.hideInputMethodManager(context, etNewPwd);
				Util.hideInputMethodManager(context, etNewPwdAgain);
				this.finish();
				break;
			case R.id.iv_om_my_wallet_pay_password_pwd_show:
				clickShowPwd();
				break;
			case R.id.btn_om_my_wallet_pay_password:
				Util.hideInputMethodManager(context, etNewPwd);
				Util.hideInputMethodManager(context, etNewPwdAgain);
				String oldPwd = etOldPwd.getText().toString().trim();
				String newPwd = etNewPwd.getText().toString().trim();
				String newPwdAgain = etNewPwdAgain.getText().toString().trim();
				if (Util.isEmpty(oldPwd))
				{
					Utils.showToast(context, "请输入旧支付密码");
				}
				else if (Util.isEmpty(newPwd))
				{
					Utils.showToast(context, "请输入6位支付密码");
				}
				else if (Util.isEmpty(newPwdAgain))
				{
					Utils.showToast(context, "请再次输入支付密码");
				}
				else if (newPwd.length() < 6 || newPwdAgain.length() < 6)
				{
					Utils.showToast(context, "密码必须为6位数字");
				}
				else if (!newPwd.equals(newPwdAgain))
				{
					Utils.showToast(context, "两次输入的密码不一致");
				}
				else if (newPwd.equals(oldPwd))
				{
					Utils.showToast(context, "新密码不能和旧密码相同");
				}
				else
				{
					ActionHelp.changePayPassword(context, oldPwd, newPwd, new ChangePasswordHandler());
				}
				break;
			default:
				break;
		}
	}
	
	private class ChangePasswordHandler
	extends
	ObjectHttpResponseHandler<String>
	{
		@Override
		public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
			Log.i(responseString);
			if (statusCode == 9101 || statusCode == 9102)
			{
				try
                {
                	JSONObject jsonObject = new JSONObject(responseString);
	                String tip = jsonObject.getString("returnObject");
	                Util.showToast(context, tip);
                }
                catch (JSONException e)
                {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
                }
				return;
			}
			if (statusCode < 1000)
			{
				Utils.ToastShow(OrderMenuPayChangePasswordActivity.this, "网络错误");
			}
			else
			{
				Utils.ToastShow(OrderMenuPayChangePasswordActivity.this, "请求出错");
			}
		}
		
		@Override
		public void onSuccess(int statusCode, Header[] headers, String object, String responseString) {
			Log.i(responseString);
			if (object.equals("支付密码修改成功"))
			{
				Util.showToast(context, "修改成功");
				OrderMenuPayChangePasswordActivity.this.finish();
			}
		}
		
		@Override
		public Type getType() {
			return new TypeToken<String>()
					{
					}.getType();
		}
	}
	
	private void clickShowPwd() {
		if(Is_SHOW_PWD){
			//切换成密文显示
			   Is_SHOW_PWD = false;
			   ivShow.setSelected(false);
			   etNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());				  
			   etNewPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());				  
		   } else {
			 //切换成明文显示
			   Is_SHOW_PWD = true;
			   ivShow.setSelected(true);
			   etNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());	
			   etNewPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());	
		   }	    
    }
	
	@Override
	public Handler getUIHandler() {
		return handler;
	}
	
}
